(function () {
  'use strict';

  angular
    .module('client')
    .controller('ControlController', ControlController);

  ControlController.$inject = ['$scope', '$stateParams', '$state', '$interval', '$window', '$document', 'controlService', 'indexService'];
  function ControlController($scope, $stateParams, $state, $interval, $window, $document, controlService, indexSevice) {
    if (!controlService.getPhoneScreenSize()) {
      $state.go('device');
    }

    var vm = this;
    var  poll = undefined;

    vm.deviceModel = '未获取';
    vm.time = {
      over: false,
      minute: '00',
      second: '00',
    };

    vm.controlToolbar = controlToolbar;
    vm.releaseDevice = releaseDevice;
    vm.keycode = controlService.keycode;

    $scope.$on('$destroy', destroy);
    $window.onunload = unload;
    $window.onbeforeunload = beforeunload;
    $window.onkeydown = keyCode;

    function destroy() {
      stopPoll();
      $window.onunload = undefined;
      $window.onbeforeunload = undefined;
      $window.onkeydown = undefined;
    }

    function unload(e) {
      indexSevice.clearTimer();
      controlService.releaseDevice($stateParams.deviceID)
        .then(function (resp) {
          controlService.closeWebSockets();
        })
    }

    function beforeunload(e) {
      var dialogText = '你好';
      e.returnValue = dialogText;
      return dialogText;
    }

    function keyCode(e) {
      var activeEle = $document[0].activeElement;
      if( !(activeEle && activeEle.tagName === 'INPUT')) {
        e.preventDefault();
        controlService.convertKey(e);
      }
    }

    function controlToolbar(name) {
      var toolbar = controlService.getToolbar(name);
      $state.go(toolbar);
    }

    function releaseDevice() {
      swal({
        title: "确定释放设备?",
        text: "",
        type: "warning",
        showCancelButton: true,
        confirmButtonColor: "#DD6B55",
        confirmButtonText: "确定",
        cancelButtonText: "取消",
      }, function (isConfirm) {
        if (isConfirm) {
          stopPoll();
          controlService.releaseDevice($stateParams.deviceID)
            .then(function (resp) {
              controlService.closeWebSockets();
              $state.go('device');
            })
        } else {
        }
      });
    }

    function timeOver() {
      vm.time = controlService.restTime();
      if(vm.time.over === true) {
        $window.onunload = undefined;
        $window.onbeforeunload = undefined;
        $window.onkeydown = undefined;
        stopPoll();
        controlService.closeWebSockets();
        controlService.releaseDevice($stateParams.deviceID)
          .then(function (resp) {
            $state.go('device');
          });
        //open alert window
        swal({
          title: "设备租赁期满",
          text: "",
          type: "warning",
          showCancelButton: false,
          confirmButtonColor: "#DD6B55",
          confirmButtonText: "确定",
        });
      }
    }

    function stopPoll() {
      if (angular.isDefined(poll)) {
        $interval.cancel(poll);
        poll = undefined;
      }
    }
    controlService.initPlayerStream({ctrl: vm, ele: '#player', id: $stateParams.deviceID});
    vm.deviceModel = controlService.getDeviceModel();
    vm.controlToolbar();
    poll = $interval(timeOver, 600);
  }

})();
